package by.jtraining.london.web.action;

import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.util.ServletContextAware;
import by.jtraining.london.model.User;
import by.jtraining.london.service.StudentService;
import by.jtraining.london.service.UserService;

import com.google.common.base.Preconditions;
import com.google.inject.Inject;
import com.google.inject.name.Named;

import by.jtraining.london.model.Admin;
import by.jtraining.london.model.Lector;
import by.jtraining.london.model.Student;

import com.opensymphony.xwork2.Action;

import static by.jtraining.london.constants.Constants.*;

public final class UserAction implements ServletRequestAware, ServletResponseAware,
ServletContextAware, SessionAware{

	private HttpServletRequest request;
	private HttpServletResponse response;
	private ServletContext context;
	private Map<String, Object> session;
	
	@Inject
	@Named("courseList")
	private List<String> courseList;

	@Inject
	@Named("educationList")
	private List<String> educationList;
	
	private final UserService userService;
	private final StudentService studentService;
	
	@Inject
	public UserAction(UserService userService, StudentService studentService){
		this.userService = Preconditions.checkNotNull(userService);
		this.studentService = Preconditions.checkNotNull(studentService);
	}
	
	public String execute(){
		return Action.SUCCESS;
	}

	public String login(){
		User user = userService.getUserByLoginAndPassword((String) request.getParameter(LOGIN),
										(String) request.getParameter(PASSWORD));
		if(user != null){
			session.put(USER, user);
			if (user instanceof Student){
				session.put(ROLE, STUDENT);
			}else if(user instanceof Lector){
				session.put(ROLE, LECTOR);
			}else if(user instanceof Admin){
				session.put(ROLE, ADMIN);
			}
		}
		return Action.SUCCESS;
	}
	
	public String logout(){
		request.getSession().invalidate();
		return Action.SUCCESS;
	}

//	public String registerUser(){
//		//.................
//		studentService.add(new Student)
//		return Action.SUCCESS;
//	}
	
	
//	public String userRegisterForm(){
//		return Action.SUCCESS;
//	}
	
//	public String uploadForm(){
//		//...
//		return Action.SUCCESS;
//	}
	
/*---- F  O  R     ~  A  W  A  R  E     S  E  C  T  I  O  N    S  T  A  R  T  ----*/	
	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}
	@Override
	public void setServletContext(ServletContext context) {
		this.context = context;
	}
	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}
	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}
	public HttpServletRequest getRequest() {
		return request;
	}
	public HttpServletResponse getResponse() {
		return response;
	}
	public ServletContext getContext() {
		return context;
	}
	public Map<String, Object> getSession() {
		return session;
	}
	/*------- F  O  R     ~  A  W  A  R  E     S  E  C  T  I  O  N    E  N  D  -------*/	
	
	public List<String> getCourseList() {
		return courseList;
	}
	public void setCourseList(List<String> courseList) {
		this.courseList = courseList;
	}
	public List<String> getEducationList() {
		return educationList;
	}
	public void setEducationList(List<String> educationList) {
		this.educationList = educationList;
	}

}
